<?php
/* /app/views/helpers/sla.php */
App::uses("QuestionOption","Model");
App::uses("ResponsesQuestionsOption","Model");        
class LoyaltyHelper extends AppHelper {

    function make_chart($question) {
        
        $options = QuestionOption::get_options_by_question($question['Question']['id']);
        if($question["Question"]['type']==Question::CHECKBOX){
            $legend=array();
            $data = array();
            $dat = array();
            $result= array();
            $total= 0;
            foreach($options as $option){
                $count = ResponsesQuestionsOption::count_option_in_question($question['Question']['id'], $option['Option']['id']);
                $legend[]=$option['Option']['title'];
                $data[]=$count ;
                $dat[]=$count;
                $total +=$count;
                $result[]=array('id'=>$option['Option']['id'],'option'=>$option['Option']['title'],"count"=>$count);
            }
            if($question['Question']['has_other']){
                $count = ResponsesQuestionsOption::count_option_in_question($question['Question']['id'], 0);
                $legend[]="Other";
                $data[]=  $count;
                $dat[]=$count;
                $total +=$count;
                $result[]=array('option'=>"Other","count"=>$count);
            }
            $i=0;
            foreach ($result as $value) {
                $percent = ($value['count']*100)/$total;
                $result[$i]['percent']=  round($percent);
                $i++;
            }
            $dat = array_unique($dat);
            $legend = array_reverse($legend);
            asort($dat);
            $min = min($data);
            $max= max($data);
            $url_param="&chds=$min,$max&chxt=x,y&chxl=0:|".implode('|',$dat)."|1:|".implode('|',$legend).'&chd=t:'.implode(',',$data);
            $url='http://chart.googleapis.com/chart?cht=bhs&chs=345x150'.$url_param;
            
        }else{
            $legend=array();
            $data = array();
            $result= array();
            $total= 0;
            foreach($options as $option){
                $count = ResponsesQuestionsOption::count_option_in_question($question['Question']['id'], $option['Option']['id']);
                $legend[]=$option['Option']['title']."[$count]";
                $data[]=$count ;
                $total +=$count;
                $result[]=array('id'=>$option['Option']['id'],'option'=>$option['Option']['title'],"count"=>$count);
            }
            if($question['Question']['has_other']){
                $count = ResponsesQuestionsOption::count_option_in_question($question['Question']['id'], 0);
                $legend[]="Other [$count]";
                $data[]=  $count;
                $total +=$count;
                $result[]=array('option'=>"Other","count"=>$count);
            }
            $i=0;
            foreach ($result as $value) {
                $percent = ($value['count']*100)/$total;
                $result[$i]['percent']=  round($percent);
                $i++;
            }
            $url_param="&chl=".implode('|',$legend).'&chd=t:'.implode(',',$data);
            $url='http://chart.googleapis.com/chart?cht=p&chs=345x150'.$url_param;
        }
        return array('url'=>$url,'data'=>$result);
    }
    public function get_excerpt($text,$limit=10){
        $text = strip_tags($text);
        $return = explode(" ", $text);
        $return = array_slice($return, 0, $limit);
        return implode(" ", $return).' ...';
    }
}